package com.rcw.commonlib;

import android.content.Context;
import android.os.Build;
import androidx.annotation.RequiresApi;
import androidx.fragment.app.DialogFragment;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.rcw.commonlib.fragment.FilterFragment;
import com.rcw.popup.FilterBean;
import com.rcw.popup.FilterPopupButton;
import com.rcw.popup.FilterPopupWindow;
import com.rcw.popup.FlowPopupWindow;
import com.rcw.popup.SingleFilterBean;
import com.rcw.popup.SingleFlowPopup;
import com.rcw.swiperefreshrecyclerview.SwipeRefreshListener;
import com.rcw.swiperefreshrecyclerview.SwipeRefreshRecyclerView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements SwipeRefreshListener,
        FilterPopupButton.PopupButtonMonitor,FlowPopupWindow.FlowPopupMonitor,SingleFlowPopup.SingleFlowPopupMonitor,FilterPopupWindow.FilterPopupMonitor{

    private SwipeRefreshRecyclerView swipeRefreshRecyclerView;
    private List<String> mDataList;
    private RecyclerViewAdapter mAdapter;
    private boolean isNoData = false,isFirstRefresh=false;
    private FilterPopupButton filterPopup;
    private List<List<String>> cList;
    private List<String>pList;
    private Button btn1,btn2,btn3;
    private ImageView ivArrow,ivSingleArrow,ivFilterArrow;
    private TextView tvFilter;
    private FlowPopupWindow mFixPopupWindow;
    private FilterPopupWindow mFilterPopupWindow;
    private SingleFlowPopup mSinglePopupWindow;
//    private MulFlowPopup mSinglePopupWindow;
    private Context context;
    private List<FilterBean> lists;
    private List<SingleFilterBean> listData;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context=this;
        setContentView(R.layout.activity_main);
        mDataList = new ArrayList<>();
        initData();
        swipeRefreshRecyclerView = (SwipeRefreshRecyclerView) findViewById(R.id.sf_recyclerView);
//        filterPopup=findViewById(R.id.filter_popup_button);
//        filterPopup.setValue(pList,cList);
        //筛选条件数据监听
        //filterPopup.setPopupButtonMonitor(this);
        btn1 = findViewById(R.id.btn1);
        btn2 = findViewById(R.id.btn2);
        btn3 = findViewById(R.id.btn3);
        ivFilterArrow= findViewById(R.id.iv_filter_arrow);
        ivArrow= findViewById(R.id.iv_arrow);
        ivSingleArrow= findViewById(R.id.iv_single_arrow);
        tvFilter= findViewById(R.id.tv_filter);
        btn1.setOnClickListener(new View.OnClickListener() {

            @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void onClick(View view) {
                ivFilterArrow.setImageResource(R.drawable.arrow_up_blue);
                initFilterPopup();
            }
        });

        btn2.setOnClickListener(new View.OnClickListener() {

            @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void onClick(View view) {
                ivArrow.setImageResource(R.drawable.arrow_up_blue);
                initFlowPopup();
            }
        });

        btn3.setOnClickListener(new View.OnClickListener() {

            @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void onClick(View view) {
                ivSingleArrow.setImageResource(R.drawable.arrow_up_blue);
                initSingleFlowPopup();
            }
        });

        tvFilter.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void onClick(View v) {
                FilterFragment filterFragment = FilterFragment.newInstance(pList,listData,lists);
                filterFragment.setStyle(DialogFragment.STYLE_NO_TITLE, R.style.MyDialog);
                filterFragment.show(getSupportFragmentManager(), "FilterFragment");
//                MulFilterPopup.Builder builder=new MulFilterPopup.Builder(context);
//                //设置数据
//                int width = getWindowManager().getDefaultDisplay().getWidth();
//                builder.setValues(lists,listData,(int) (width * 0.85));
//                //初始化popupWindow的相关布局及数据展示
//                builder.build();
//                //创建popup
//                MulFilterPopup mulFlowPopup=builder.createPopup();
//                //设置数据监听接口
//                //mulFlowPopup.setSingleFlowPopupMonitor(this);
//                mulFlowPopup.showAsDropDown(tvFilter);
            }
        });
        //添加HeaderView
//        View headView = View.inflate(this, R.layout.add_head_view, null);
//        swipeRefreshRecyclerView.addHeaderView(headView);
//        customRoundView=headView.findViewById(R.id.crv);

        /*//添加FooterView
        View footerView = View.inflate(this, R.layout.layout_foot_view, null);
        swipeRefreshRecyclerView.addFooterView(footerView);
        //设置EmptyView
        View emptyView = View.inflate(this, R.layout.layout_empty_view, null);
        emptyView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
        swipeRefreshRecyclerView.setEmptyView(emptyView);*/

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        //layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
//        swipeRefreshRecyclerView.setLayoutManager(layoutManager);

        GridLayoutManager gridLayoutManager=new GridLayoutManager(this,3);
        swipeRefreshRecyclerView.setLayoutManager(gridLayoutManager);
        mAdapter = new RecyclerViewAdapter(this, mDataList);
        swipeRefreshRecyclerView.setAdapter(mAdapter);
        //设置是否开启上拉加载
        swipeRefreshRecyclerView.setLoadMoreEnable(true);
        //设置是否开启下拉刷新
        swipeRefreshRecyclerView.setSwipeRefreshEnable(true);
        //设置是否显示上次刷新的时间
        swipeRefreshRecyclerView.setDisplayLastRefreshTime(true);
        //设置刷新回调
        swipeRefreshRecyclerView.setSwipeRefreshListener(this);
        //主动触发下拉刷新操作
        //swipeRefreshRecyclerView.onRefresh();

    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private void initFilterPopup() {
        FilterPopupWindow.Builder builder=new FilterPopupWindow.Builder(context);
        //设置数据
        builder.setValue(pList);
//        builder.setValue(pList,cList);
        //初始化popupWindow的相关布局及数据展示
        builder.build();
        //创建popup
        mFilterPopupWindow=builder.createPopup();
        //设置数据监听接口
        mFilterPopupWindow.setFilterPopMonitor(this);
        mFilterPopupWindow.showAsDropDown(btn1);
        Log.e("rcw","dffhyhuh");
        mFilterPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                ivFilterArrow.setImageResource(R.drawable.arrow_down);
            }
        });
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private void initFlowPopup() {
        FlowPopupWindow.Builder builder=new FlowPopupWindow.Builder(context);
        //设置数据
        builder.setValues(lists);
        //设置标签字体的颜色，这里的color不是values目录下的color,而是res文件夹下的color
        builder.setLabelColor(R.color.color_popup);
        //设置标签的背景色
        //builder.setLabelBg(R.drawable.flow_label);
//        builder.setBtnBg(R.drawable.shape_circle_bg);
        //设置GridLayout的列数
        builder.setColumnCount(4);
        //初始化popupWindow的相关布局及数据展示
        builder.build();
        //创建popup
        mFixPopupWindow=builder.createPopup();
        //设置数据监听接口
        mFixPopupWindow.setFlowPopupMonitor(this);
        mFixPopupWindow.showAsDropDown(btn2);
        mFixPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                ivArrow.setImageResource(R.drawable.arrow_down);
            }
        });
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private void initSingleFlowPopup() {
        SingleFlowPopup.Builder builder=new SingleFlowPopup.Builder(context);
        //设置数据
        builder.setValues(listData);
        //设置标签字体的颜色，这里的color不是values目录下的color,而是res文件夹下的color
        builder.setLabelColor(R.color.color_popup);
        //设置标签的背景色
        builder.setLabelBg(R.drawable.flow_popup);
        //设置GridLayout的列数
        builder.setColumnCount(3);
        //初始化popupWindow的相关布局及数据展示
        builder.build();
        //创建popup
        mSinglePopupWindow=builder.createPopup();
        //设置数据监听接口
        mSinglePopupWindow.setSingleFlowPopupMonitor(this);
        mSinglePopupWindow.showAsDropDown(btn3);
        mSinglePopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                ivSingleArrow.setImageResource(R.drawable.arrow_down);
            }
        });
    }

    @Override
    public void onRefresh() {
        swipeRefreshRecyclerView.postDelayed(new Runnable() {
            @Override
            public void run() {
                //mDataList.clear();
                if (isFirstRefresh){
                    mDataList.add(0,"新增刷新测试数据01");
//                   mDataList.add("新增刷新测试数据02");
//                   mDataList.add("新增刷新测试数据03");

                }/*else {
                    for (int i = 0; i < 20; i++) {
                        mDataList.add("测试数据" + i);
                    }
                }*/
                //swipeRefreshRecyclerView.setRefreshFail();
                swipeRefreshRecyclerView.setRefreshComplete();

                if (isFirstRefresh){
                    mAdapter.notifyItemInserted(0);
                    swipeRefreshRecyclerView.scrollToPosition(1);
                }else {
                    isFirstRefresh=true;
                    mAdapter.notifyDataSetChanged();
                }
            }
        }, 2000);
    }

    private void initData() {
        for (int i = 0; i < 20; i++) {
            mDataList.add("测试数据" + i);
        }

        pList = new ArrayList<>();
        cList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            if (i == 0) {
                pList.add("全部年级");
                List<String> t = new ArrayList<>();
                cList.add(t);
            } else {
                pList.add(i + "年级");
                List<String> t = new ArrayList<>();
                for (int j = 0; j < 15; j++) {
                    if (j < 9) {
                        t.add(i + "-0" + (j + 1) + "班");
                    } else t.add(i + "-" + (j + 1) + "班");
                }
                cList.add(t);
            }

        }
        listData=new ArrayList<>();
        List<SingleFilterBean.TableMode> list0=new ArrayList<>();
        list0.add(new SingleFilterBean.TableMode("全部"));
        list0.add(new SingleFilterBean.TableMode("超A"));
        list0.add(new SingleFilterBean.TableMode("A类"));
        list0.add(new SingleFilterBean.TableMode("B类"));
        list0.add(new SingleFilterBean.TableMode("C类"));
        list0.add(new SingleFilterBean.TableMode("D类"));
        list0.add(new SingleFilterBean.TableMode("E类"));
        listData.add(new SingleFilterBean(new SingleFilterBean.TableMode("全部"),list0));


        //流式布局数据
        lists=new ArrayList<>();
        List<FilterBean.TableMode> list=new ArrayList<>();
        list.add(new FilterBean.TableMode("不限"));
        list.add(new FilterBean.TableMode("已开通"));
        list.add(new FilterBean.TableMode("未开通"));
        lists.add(new FilterBean("业务状态",new FilterBean.TableMode("不限"),list));
        List<FilterBean.TableMode> list1=new ArrayList<>();
        list1.add(new FilterBean.TableMode("不限"));
        list1.add(new FilterBean.TableMode("已打印"));
        list1.add(new FilterBean.TableMode("未打印"));
        lists.add(new FilterBean("打印状态",new FilterBean.TableMode("不限"),list1));
        List<FilterBean.TableMode> list2=new ArrayList<>();
        list2.add(new FilterBean.TableMode("不限"));
        list2.add(new FilterBean.TableMode("未知"));
        list2.add(new FilterBean.TableMode("制卡中"));
        list2.add(new FilterBean.TableMode("已完成"));
        lists.add(new FilterBean("制卡状态",new FilterBean.TableMode("不限"),list2));
        List<FilterBean.TableMode> list3=new ArrayList<>();
        list3.add(new FilterBean.TableMode("不限"));
        list3.add(new FilterBean.TableMode("未知"));
        list3.add(new FilterBean.TableMode("已订购"));
        list3.add(new FilterBean.TableMode("未订购"));
        lists.add(new FilterBean("订购状态",new FilterBean.TableMode("不限"),list3));
        List<FilterBean.TableMode> list4=new ArrayList<>();
        list4.add(new FilterBean.TableMode("不限"));
        list4.add(new FilterBean.TableMode("是"));
        list4.add(new FilterBean.TableMode("否"));
        lists.add(new FilterBean("黑名单",new FilterBean.TableMode("不限"),list4));
    }

    private int j=1;
    @Override
    public void onLoadMore() {
        swipeRefreshRecyclerView.postDelayed(new Runnable() {
            @Override
            public void run() {
                //模拟加载了所有数据
                if (j>2){
                    isNoData=true;
                }else {
                    j++;
                    for (int i = 0; i < 10; i++) {
                        mDataList.add("新增测试数据" + j+i);
                    }
                }
                //swipeRefreshRecyclerView.setLoadMoreFail();
                swipeRefreshRecyclerView.setLoadMoreComplete(isNoData);
                mAdapter.notifyDataSetChanged();
            }
        }, 2000);

    }

    /*@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
            if (mFixPopupWindow != null && mFixPopupWindow.isShowing()) {
                mFixPopupWindow.dismiss();
                return false;
            } else if (filterPopup.getPopupWindow() != null && filterPopup.getPopupWindow().isShowing()) {
                filterPopup.getPopupWindow().dismiss();
                return false;
            }
        }
        return super.onKeyDown(keyCode, event);
    }*/

    @Override
    public void setFilterResult(String filterResult) {
        Log.e("rcw","filterResult--->"+filterResult);
        Toast.makeText(this,filterResult,Toast.LENGTH_LONG).show();
//        mDataList.clear();
//        for (int i = 0; i < 20; i++) {
//            mDataList.add("重新获取数据" + i);
//        }
//        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void setFlowPopupResult(List<String> filterResult) {
        Toast.makeText(this,filterResult.toString(),Toast.LENGTH_LONG).show();
    }

    @Override
    public void setSingleFlowPopupResult(String filterResult) {
        Toast.makeText(this,filterResult,Toast.LENGTH_LONG).show();
    }

//    @Override
//    public void setFlowPopupResult(String filterResult) {
//        Log.e("rcw","filterResult===>"+filterResult);
//    }
}
